﻿using System.Linq;
using AutoMapper;
using API.Helpers;
using API.IRepositories;
using API.IServices;
using API.Models;
using API.Profiles;
using AutoMapper.QueryableExtensions;
using Helper.Exceptions;
using LinqKit;

namespace API.Services
{
    public class DrinkService : EndUserProfileService, IDrinkService
    {
        private readonly IDrinkRepository _drinkRepository;
    private readonly bool _validateCustomService;

    public DrinkService()
        : this(DependencyInjectionHelper.Resolve<IEndUserProfile>(), DependencyInjectionHelper.Resolve<IDrinkRepository>())
    {
    }

    public DrinkService(IEndUserProfile authenticatedUserProfile, IDrinkRepository drinkRepository, bool validateCustomService = true)
        : base(authenticatedUserProfile)
    {
        _drinkRepository = drinkRepository;
        _validateCustomService = validateCustomService;
    }
    

     

        //public Drink Save(Drink entity, Type baseEmptyEntityOnType)
        //{
        //    throw new NotImplementedException();
        //}





    public void Delete(int id)
    {
        var model = Get(id);
        if (model == null)
            throw new ValidationException("The requested Drink was not found and could not be deleted.");

        _drinkRepository.Delete(id);
    }

    public IQueryable<Models.Drink> Get()
    {
        return _drinkRepository
            .Get()
            .AsExpandable()
            .OrderBy(e => e.Name)
            .Project()
            .To<Models.Drink>();
    }

    public Models.Drink Get(int id)
    {
        return this.Get().FirstOrDefault(c => c.Id == id);
    }



    public Models.Drink Save(Models.Drink entity)
    {
       // ValidationHelper.ValidateModel(entity, _validateCustomService);

        return SaveSupplier(entity);
    }

    private Drink SaveSupplier(Drink entity)
    {
        return Mapper.Map<EntityModels.Drink, Models.Drink>(_drinkRepository.Save(Mapper.Map<Models.Drink, EntityModels.Drink>(entity), typeof(Models.Drink)));
    }
    }
}

